<?php

/**
 * Login short summary.
 *
 * Login description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Customer_model');
        $this->load->library('form_validation');
    }
    public function lr()
    {
        $this->load->view('Pre/login/lr.html');
    }

    public function index()
    {
        $this->load->view('Pre/login/index.html');
    }
    //重新登录页面



    public function register()
    {
        $this->load->view('Pre/login/register.html');
    }

    public function success_register()
    {
        $this->load->view('Pre/login/success_register.html');
    }
    //注册失败 register_fail
    public function register_fail()
    {
        $this->load->view('Pre/login/register_fail.html');
    }
    //注册
    public function do_register()
    {
        $valiRes= $this->form_validation->run('account_vali');
        $this->load->library('ajaxMsgModel');
        if($valiRes)
        {
            $this->Customer_model->account=$this->input->post('account');
            $this->Customer_model->phone=$this->input->post('phone');
            $this->Customer_model->pass=md5($this->input->post('pass'));
            $this->Customer_model->email=$this->input->post('email');
            $this->Customer_model->addtime=$this->input->post('addtime');
            $customerModel=$this->Customer_model;
            $res= $this->Customer_model->inset_customer($customerModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='注册成功';
                $this->ajaxmsgmodel->Statu='ok';

            }
            else
            {
                $this->ajaxmsgmodel->Msg='注册失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else
        {
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    //登陆
    public function do_login()
    {
        $accountModel=new Customer_model();
        $this->load->library('ajaxMsgModel');
        $accountModel->account=$this->input->post('account');
        $accountModel->pass=md5($this->input->post('pass'));
        $this->load->library('session');
        $accountModel=$this->Customer_model->get_login_account($accountModel);
        $this->ajaxmsgmodel->Statu='err';
        if(count($accountModel)==1)
        {
            $this->session->set_userdata(get_object_vars($accountModel));
            $this->ajaxmsgmodel->Statu='ok';
        }
       else
        {
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
    //登陆成功提示
    public function login_success()
    {
        $data['backurl']=$this->input->get('backurl');
        $this->load->view('Pre/login/login_success.html',$data);
    }
    public function login_fail()
    {
        $this->load->view('Pre/login/login_fail.html');
    }
    public function signout()
    {
        $this->load->library('session');
        if($this->session->has_userdata('account'))
        {
            $this->session->unset_userdata(array('customer_id','account','pass','phone','email','addtime'));
            $this->load->library('ajaxMsgModel');
            if($this->session->has_userdata('account'))
            {
                $this->ajaxmsgmodel->Statu='err';
            }else
            {
                $this->ajaxmsgmodel->Statu='ok';
            }
        }else
        {
            $this->ajaxmsgmodel->Statu='out';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}